<template>
  <el-dialog :visible.sync="dialog" width="550px" :close-on-click-modal="false" :before-close="reset" destroy-on-close>
    <span slot="title" class="dialog-title">{{ title }}</span>
    <div class="info">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="任务名：" prop="jobName">
          <el-input v-model="form.jobName" placeholder="任务名" clearable :disabled="Boolean(form.id)" />
        </el-form-item>
        <el-form-item label="任务配置：" prop="taskConfigId">
          <el-select v-if="form.id===1" v-model="form.taskConfigId" placeholder="任务配置" :disabled="form.id===1">
            <el-option label="中心服务器" :value="0" />
          </el-select>
          <el-select v-else v-model="form.taskConfigId" placeholder="任务配置" :disabled="Boolean(form.id)">
            <el-option v-for="item in taskConfigList" :key="item.id" :label="item.serviceName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="cron表达式：" prop="cronExpression">
          <div class="cron">
            <el-popover v-model="cronPopover">
              <cron i18n="cn" @change="changeCron" @close="cronPopover=false" />
              <el-input slot="reference" v-model="form.cronExpression" placeholder="请输入定时策略" @focus="cronPopover=true" />
            </el-popover>
          </div>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="描述" clearable />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="reset">取 消</el-button>
      <el-button type="primary" size="small" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getConfigs, addTask, updateTask } from '@/api/timedTask'
import { cron } from 'vue-cron'
export default {
  components: { cron },
  props: {
    dialog: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      cronPopover: false,
      form: {
        jobName: '',
        taskConfigId: '',
        cronExpression: '',
        id: null,
        remark: '',
        status: 1
      },
      title: '添加',
      taskConfigList: [],
      rules: {
        jobName: [
          { required: true, message: '请输入任务名', trigger: 'blur' }
        ],
        taskConfigId: [
          { required: true, message: '请选择任务配置', trigger: 'change' }
        ],
        cronExpression: [
          { required: true, message: '请选择或输入cron表达式', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    dialog(dialog) {
      if (dialog) {
        this.getOptions()
        if (Object.keys(this.row).length === 0) {
          this.$data.form = this.$options.data().form
          this.title = '添加'
        } else {
          this.title = '修改'
          for (const key in this.form) {
            this.form[key] = this.row[key]
          }
        }
        setTimeout(() => {
          this.$refs.form.clearValidate()
        }, 10)
      }
    }
  },
  methods: {
    reset() {
      this.$emit('reset', 'dialog')
    },
    changeCron(val) {
      this.form.cronExpression = val
    },
    submit() {
      const _this = this
      _this.$refs.form.validate((valid) => {
        if (valid) {
          const data = {
            ..._this.form,
            jobGroup: 'jobGroup',
            beanClass: 'com.itens.backup.job.CopeDataJob'
          }
          console.log(data)
          if (data.id) {
            updateTask(data).then(res => {
              if (res.code * 1 === 200) {
                this.$message({
                  message: `${this.title}成功`,
                  type: 'success',
                  showClose: true
                })
                this.$emit('submit', 'dialog')
              }
            })
            return
          }
          addTask(data).then(res => {
            if (res.code * 1 === 200) {
              this.$message({
                message: `${this.title}成功`,
                type: 'success',
                showClose: true
              })
              this.$emit('submit', 'dialog')
            }
          })
        } else {
          return false
        }
      })
    },
    getOptions() {
      const data = {
        page: 1,
        pageSize: 9999,
        status: 1
      }
      getConfigs(data).then(res => {
        this.taskConfigList = res.data.row
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.info {
  padding: 24px 24px 0 24px;
  border: 1px solid #e1e5ed;
  border-radius: 0px 0px 4px 4px;
}
.label {
  font-size: 14px;
  font-weight: 400;
}
.el-input,
.el-select {
  width: 330px;
}
</style>
